package com.ruiyian.project.plan.controller;

import com.ruiyian.common.core.controller.BaseController;
import com.ruiyian.common.core.domain.AjaxResult;
import com.ruiyian.common.core.page.TableDataInfo;
import com.ruiyian.project.plan.domain.Plan;
import com.ruiyian.project.plan.service.PlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2021/1/29
 * Desc: 预案管理
 */
@Slf4j
@RestController
@RequestMapping("/plan/info")
public class PlanController extends BaseController {
    @Autowired
    private PlanService planService;
    /**
     * 预案列表
     * @param plan
     * @return
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Plan plan) {
        startPage();
        List<Plan> list = planService.selectList(plan);
        return getDataTable(list);
    }

    /**
     *  列表不分页
     * @param plan
     * @return
     */
    @PostMapping("/selectList")
    public AjaxResult selectList(@RequestBody Plan plan) {
        List<Plan> list = null;
        try {
            list = planService.selectList(plan);
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
            logger.error(e.getMessage());
        }
        return AjaxResult.success(list);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Long id) {
        try {
            planService.delete(id);
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 保存
     * @param plan
     * @return
     */
    @PutMapping("/save")
    public AjaxResult save(@RequestBody Plan plan) {
        try {
            if(plan.getId()!=null){
                planService.edit(plan);
            }else {
                planService.save(plan);
            }
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 根据id获取对象
     * @param id
     * @return
     */
    @PostMapping("/findOne")
    public AjaxResult findOne(@RequestBody Long id) {
        Plan plan = null;
        try {
            plan = planService.findOne(id);
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(plan);
    }

    /**
     * 批量删除
     * @param id
     * @return
     */
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(@RequestBody List<Plan> id) {
        try {
            planService.deleteBatch(id);
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 查询预案 根据隧道id和预案编码
     * @param plan
     * @return
     */
    @PostMapping("/getPlanInfo")
    public AjaxResult getPlanInfo(@RequestBody Plan plan) {
        Plan planInfo = null;
        try {
            planInfo = planService.getPlanInfo(plan);
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(planInfo);
    }

    /**
     * 根据隧道点位获取预案
     * @param tunnelId
     * @return
     */
    @PostMapping("/getPlanInfoByTunnel")
    public AjaxResult getPlanInfo(@RequestBody Long tunnelId) {
        Plan planInfo = null;
        try {
            planInfo = planService.getPlanInfoByTunnel(tunnelId);
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(planInfo);
    }
}
